define(function(require, exports, module) {

	FXREADER.BA_SELANNOT_TOOLNAME = "Annot Selection Tool";
	
	FXREADER.Event.AnnotSelectionToolHandler = function(pReaderApp,pAnnotHandleManager) {
		
		var _pReaderApp = pReaderApp,
			_pAnnotHandleManager = pAnnotHandleManager,
			_currentPageView = null,
			_startwindowpoint = new FXREADER.CPDF_Point(0, 0),
            _rcDraw = null,
            _boundSelectionRectDiv = null, //标记范围选择区域Div元素jQuery对象
			_bMouseDown = false;

		this.Tool_OnInit = function(pReaderApp) {
		};

		this.Tool_GetName = function() {
			return FXREADER.BA_SELANNOT_TOOLNAME;
		};

		// UI Events
		this.Tool_OnActivate = function() {
			if (_pReaderApp.GetMainView() != null) {
				var pDocView = _pReaderApp.GetMainView().GetDocView();
				var jqDocViewContainer = $("#"
						+ pDocView.GetDocViewContainerID());
				jqDocViewContainer.css({
					'cursor' : "auto"
				});
			}
			var pSelAnnotCommand = _pReaderApp.GetCommand("SelectAnnotCommand");
			pSelAnnotCommand && pSelAnnotCommand.SetState(true);
		};

		this.Tool_OnDeactivate = function() {
			var pSelAnnotCommand = _pReaderApp.GetCommand("SelectAnnotCommand");
			pSelAnnotCommand && pSelAnnotCommand.SetState(false);
		};

		this.Tool_IsEnabled = function() {
			var pCurrentDocument = _pReaderApp.GetMainView().GetDocView()
					.GetPDFDoc();
			if (pCurrentDocument) {
				if (pCurrentDocument.GetDocType() != FXREADER.PDFData.DocType.XFA
						/*&& _pReaderApp.GetDocPemission&FPDFPERM_ANNOT_FORM != 0*/)
					return true;
			}
			return false;
		};

		this.Tool_IsProcessing = function() {
			return _bMouseDown;
		};

		// Mouse events
		this.Tool_OnLButtonDown = function(event) {
			var jqDocViewContainer = $("#" + pDocView.GetDocViewContainerID());
			if (!FXREADER.Common.ContainsNode(jqDocViewContainer.get(0),
					event.target, true)) {
				return false;
			}
			var pDocView = _pReaderApp.GetMainView().GetDocView();
			var bShift = event.shiftKey;
			var bCtrl = event.ctrlKey;
			if(!bShift && !bCtrl)
			{
				pDocView.ClearAllSelection();
				_pAnnotHandleManager.ClearSelection();
				//pDocView.SetFocusAnnot(NULL);
			}
			
			_pAnnotHandleManager.ClearDragRects();
			
			var pPageView = null, ptOffsetCurPageView = new FXREADER.CPDF_Point(
					0, 0);
			pPageView = FXREADER.TextSelect.CReader_TextSelectTool
					.GetPageViewInfoByMouseEvent(_pReaderApp, event,
							ptOffsetCurPageView, true, false);
			if (pPageView == null)
				return false;
			
			_bMouseDown = true;
			_currentPageView = pPageView;
			_startwindowpoint.x = event.pageX;
			_startwindowpoint.y = event.pageY;
			_rcDraw = FXREADER.CPDF_Rect(0,0,0,0);
			return true;
		};

		this.Tool_OnLButtonUp = function(event) {
			if(_bMouseDown)
			{
				 _HideBoundBox();
				_pAnnotHandleManager.SelectAnnots(_currentPageView, _rcDraw);
			}
			
			_bMouseDown = false;
			FXREADER.RectUtils.Empty(_rcDraw);
			_startwindowpoint.x = 0;
			_startwindowpoint.y = 0;
			_currentPageView = null;
			return true;
		};
	
		this.Tool_OnLButtonDblClk = function(event) {
			return false;
		};

		this.ClipSelectRect = function(pPageView, rcRect)
		{
			var rcPage = pPageView.GetPageViewRect();
			rcRect.left = (rcRect.left > rcPage.left ? rcRect.left : rcPage.left);
			rcRect.top = (rcRect.top > rcPage.top ? rcRect.top : rcPage.top);
			rcRect.right = (rcRect.right > rcPage.right ? rcPage.right : rcRect.right);
			rcRect.bottom = (rcRect.bottom > rcPage.bottom ? rcPage.bottom : rcRect.bottom);
		};
		
		function _DrawBoundBox(drawRect) {
			FXREADER.RectUtils.Normalize();
            var jqBoundSelection = _GetBoundSelectionRectDiv();
            var x = FXREADER.RectUtils.Width(drawRect);
            var y = FXREADER.RectUtils.Height(drawRect);

            var pDocView = _pReaderApp.GetMainView().GetDocView();
            var docViewRect = pDocView.GetDocViewClientRect();
            var ScrollApi = pDocView.GetScrollApi();
            var scrollPosX = 0, scrollPosY = 0;
            if (ScrollApi) {
                scrollPosX = ScrollApi.getContentPositionX();
                scrollPosY = ScrollApi.getContentPositionY();
            }
            var l = drawRect.left;
            var  t = drawRect.bottom;
            l = l - docViewRect.left + scrollPosX;
            t = t - docViewRect.bottom + scrollPosY;
            jqBoundSelection.css({ left: l, top: t, width: x, height: y });
            jqBoundSelection.removeClass("fwr_hidden");
        }
		
	    function _HideBoundBox() {
	         var jqBoundSelection = _GetBoundSelectionRectDiv();
	         jqBoundSelection.addClass("fwr_hidden");
	    }
	    
	    function _GetBoundSelectionRectDiv() {
            if (_boundSelectionRectDiv == null) {
                var pDocView = _pReaderApp.GetMainView().GetDocView();
                var pFirstPageView = pDocView.GetPageView(0);
                var jqPagesContainerDiv = $("#" + pDocView.GetDocViewContainerID());
                var selectionDivID = pDocView.GetDocViewContainerID() + "_annot_bound_select";
                var divHtml = "<div class='fwr_text_bound_selection fwr_hidden' id='" + selectionDivID + "'></div>";
                if (pFirstPageView != null && pFirstPageView.IsContentCreated()) {
                    $("#" + pFirstPageView.GetPageContainerID()).parent().parent().append(divHtml);
                }
                else {
                    jqPagesContainerDiv.append(divHtml);
                }
                _boundSelectionRectDiv = $("#" + selectionDivID);
            }
            return _boundSelectionRectDiv;
        }
		
		this.Tool_OnMouseMove = function(event) {
			if(_bMouseDown)
			{
				_rcDraw = new FXREADER.CPDF_Rect(_startwindowpoint.x,_startwindowpoint.y,event.pageX,event.pageY);
				_self.ClipSelectRect(pPageView,_rcDraw);
				_pAnnotHandleManager.ClearDragRects();
				_pAnnotHandleManager.AddDragRect(_currentPageView, _rcDraw);
			}
			return true;
		};

		this.Tool_OnRButtonDown = function(event) {
			return false;
		};

		this.Tool_OnRButtonUp = function(event) {
			return false;
		};

		this.Tool_OnRButtonDblClk = function(event) {
			return false;
		};

		this.Tool_OnMouseWheel = function(event) {
			return false;
		};

		this.Tool_OnMouseOver = function(event) {
			return false;
		};

		this.Tool_OnMouseOut = function(event) {
			return false;
		};
	
		this.Tool_OnMouseEnter = function(event) {
			return false;
		};

		this.Tool_OnMouseLeave = function(event) {
			return false;
		};
	};
	return FXREADER.Event.AnnotSelectionToolHandler;
});